//
//  TMGMineOrderViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/11.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGMineOrderViewController.h"
#import "TMGOrderTableViewCell.h"
#import "TMGOrderDetailViewController.h"
#import "TMGOrderModel.h"
@interface TMGMineOrderViewController ()
@property(nonatomic, strong) NSArray                               *titleArray;
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) STNoresultView                     *noResultView;
@property(nonatomic, strong) NSMutableArray<TMGOrderModel*>                     *dataSouce;
@end

@implementation TMGMineOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArray = @[@"买金",@"卖金",@"投资",@"赎回",@"充值",@"提现"];
    //隐藏 投资和赎回
    _titleArray = @[@"买金",@"卖金",@"充值",@"提现"];
    self.title = @"我的订单";
    [self configSubView];
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无该类订单" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendOrderListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.view.backgroundColor = [UIColor whiteColor];
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth, 44)
                                                         andTitle:_titleArray handle:^(STSegementButton *sender) {
                                                             [weakSelf.tableView.mj_header beginRefreshing];
                                                         }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = TM_orangeColor;
    self.segementButton.butTitleSelectedColor = TM_orangeColor;
    self.segementButton.butTitleColor = TM_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    [self.view addSubview:self.segementButton];
    
    self.tableView.st_top = self.segementButton.bottom;
    self.tableView.st_height =  [TMUtils tabBarTop] - self.segementButton.bottom + 49;
    //无数据view
    self.noResultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 350) title:@"暂无订单" buttonTitle:@"快去购买商品吧" buttonHandle:^(NSString *titleString) {
        
    }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];

}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGOrderTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    TMGOrderModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMGOrderModel * model = self.dataSouce[indexPath.row];
    TMGOrderDetailViewController * vc = [TMGOrderDetailViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendOrderListRequest{
    //1买金2卖金3投资4赎回6充值7提现
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSString *trade_type ;
    NSString * name = self.titleArray[self.segementButton.cureentIndex];
    if ([name isEqualToString:@"买金"]) {
        trade_type = @"1";
    }
    if ([name isEqualToString:@"卖金"]) {
        trade_type = @"2";
    }
    if ([name isEqualToString:@"投资"]) {
        trade_type = @"3";
    }
    if ([name isEqualToString:@"赎回"]) {
        trade_type = @"4";
    }
    if ([name isEqualToString:@"充值"]) {
        trade_type = @"6";
    }
    if ([name isEqualToString:@"提现"]) {
        trade_type = @"7";
    }
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:trade_type forKey:@"trade_type"];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/order/getOrders"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"][@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 for (NSDictionary *modelDic in array) {
                                                     TMGOrderModel * model = [TMGOrderModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 self.dataSouce = [NSMutableArray new];
                                                 [self.tableView reloadData];
                                                //[SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
